package com.zhuanzhuan.hero.user.helper;

import com.bj58.youpin.common.rpc.result.Result;
import com.bj58.youpin.common.utils.json.JsonUtils;
import com.zhuanzhuan.hero.biz.common.util.WxWarnTemplateUtil;
import com.zhuanzhuan.hero.recycle.order.enums.PortValueEnum;
import com.zhuanzhuan.hero.recycle.order.facade.IStatisticsFacade;
import com.zhuanzhuan.hero.recycle.order.vo.UserCenterSellAndRecycleCountVO;
import com.zhuanzhuan.hero.user.consts.Consts;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Map;

/**
 * @ClassName HeroRecycleOrderHelper
 * @Description
 * @Author hanjunjun
 * @Date 2024/7/5 17:06
 */
@Component
@Slf4j
public class HeroRecycleOrderHelper {

    @Resource
    private IStatisticsFacade statisticsFacade;

    /**
     * 查询交易订单总数
     *
     * @param fromUid
     * @param toUid
     * @return java.lang.Long
     * @author  hanjunjun
     * @date 2024/7/9
     **/
    public Long statisticsOrderNum(Long fromUid, Long toUid) {
        log.info("getTransactionNumberMap fromUid={}, toUid={}", fromUid, toUid);
        Result<Map<Long, Long>> result =
            statisticsFacade.getTransactionNumberMap(fromUid, Collections.singletonList(toUid), PortValueEnum.NOT_MINI_PROGRAM);
        log.info("getTransactionNumberMap result={},", JsonUtils.toJsonWithoutNull(result));

        if (!result.isSuccess()) {
            WxWarnTemplateUtil.warnOutInvokedService(Consts.SERVICE_NAME, Consts.RECYCLE_ORDER_SERVICE_NAME, "statisticsOrderNum调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }

        //k:uid v:num
        Map<Long, Long> data = result.getData();
        return data.get(toUid);
    }

    /**
     * 查询用户的回收单数和送检卖出的订单列表
     *
     * @param uid
     * @return com.zhuanzhuan.hero.recycle.order.vo.UserCenterSellAndRecycleCountVO
     * @author  hanjunjun
     * @date 2024/7/9
     **/
    public UserCenterSellAndRecycleCountVO querySellAndRecycleCount(Long uid) {
        log.info("getUserCenterSellAndRecycleCountV1 uid={}", uid);
        Result<UserCenterSellAndRecycleCountVO> result =
            statisticsFacade.getUserCenterSellAndRecycleCountV1(uid);
        log.info("getUserCenterSellAndRecycleCountV1 result={},", JsonUtils.toJsonWithoutNull(result));

        if (!result.isSuccess()) {
            WxWarnTemplateUtil.warnOutInvokedService(Consts.SERVICE_NAME, Consts.RECYCLE_ORDER_SERVICE_NAME, "querySellAndRecycleCount调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }

        return result.getData();
    }

    /**
     * 7天内是否有未完成的订单
     *  
     * @param uid
     * @return boolean
     * @author  hanjunjun  
     * @date 2024/7/12       
     **/
    public boolean hasCompletedOrderInSevenDays(Long uid) {
        log.info("hasCompletedOrderInSevenDays uid={}", uid);
        Result<Boolean> result = statisticsFacade.hasCompletedOrderInSevenDays(uid);
        log.info("hasCompletedOrderInSevenDays result={},", JsonUtils.toJsonWithoutNull(result));

        if (!result.isSuccess()) {
            WxWarnTemplateUtil.warnOutInvokedService(Consts.SERVICE_NAME, Consts.RECYCLE_ORDER_SERVICE_NAME, "hasCompletedOrderInSevenDays调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }

        return result.getData();
    }

    /**
     * 是否存在交易未完成的回收单
     *
     * @param uid
     * @return boolean
     * @author  hanjunjun
     * @date 2024/7/12
     **/
    public boolean hasUnfinishedOrder(Long uid) {
        log.info("hasUnfinishedOrder uid={}", uid);
        Result<Boolean> result = statisticsFacade.hasUnfinishedOrder(uid);
        log.info("hasUnfinishedOrder result={},", JsonUtils.toJsonWithoutNull(result));

        if (!result.isSuccess()) {
            WxWarnTemplateUtil.warnOutInvokedService(Consts.SERVICE_NAME, Consts.RECYCLE_ORDER_SERVICE_NAME, "hasUnfinishedOrder调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }

        return result.getData();
    }
}
